package com.amigo.online.provider.manager.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.provider.manager.model.entity.CategoryTbl;
import com.amigo.online.provider.manager.service.CategoryTblService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@RestController
@RequestMapping(value = "/category", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class CategoryTblController extends BaseController {
    @Resource
    private CategoryTblService categoryTblService;

    @PostMapping(value = "/getCategoryList")
    public ResponseEntity<Response> getCategoryList(@RequestBody CategoryTbl categoryTbl) {
        JSONObject result = new JSONObject();

        // 分页语句
        if (categoryTbl.getPageCount() == null || categoryTbl.getPageCount() <= 0) {
            categoryTbl.setPageCount(10);
        }
        Page<Object> page = PageHelper.startPage(categoryTbl.getPageNo(), categoryTbl.getPageCount());
        List<CategoryTbl> categoryTblList = categoryTblService.findCategoryList(categoryTbl);

        result.put("categoryTblList", categoryTblList);
        result.put("pagetotal", page.getTotal());
        return success(result);
    }

    @PostMapping(value = "/saveCategory")
    public ResponseEntity<Response> saveCategory(@RequestBody CategoryTbl categoryTbl) {
        return success(categoryTblService.saveCategory(categoryTbl));
    }

    @PutMapping(value = "/updateCategory")
    public ResponseEntity<Response> updateCategory(@RequestBody CategoryTbl categoryTbl) {
        return success(categoryTblService.updateCategory(categoryTbl));
    }

    @PutMapping(value = "/deleteCategory")
    public ResponseEntity<Response> deleteCategory(@RequestBody CategoryTbl categoryTbl) {
        return success(categoryTblService.deleteCategory(categoryTbl));
    }

}
